package com.example.hao.imageselector;

import android.content.Intent;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.donkingliang.imageselector.utils.ImageSelector;
import com.lhm.easypermissionsactivity.EasyPermissionActivity;
import com.liyi.viewer.ImageLoader;
import com.liyi.viewer.ImageViewerUtil;
import com.liyi.viewer.ViewData;
import com.liyi.viewer.widget.ImageViewer;

import java.util.ArrayList;

public class MainActivity extends EasyPermissionActivity {

    private int REQUEST_CODE = 10;
    private RecyclerView rv;
    private RVImageAdapter mAdapter;
    private ImageViewer imagePreview;
    private ArrayList<ViewData> mViewDatas;
    private Point mScreenSize;
    private ArrayList<String> images;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        requestPermissions();
        initView();
    }

    private void initView() {

        rv = findViewById(R.id.rv);
        rv.setLayoutManager(new GridLayoutManager(this, 3));

        mAdapter = new RVImageAdapter(this);
        rv.setAdapter(mAdapter);

        imagePreview = findViewById(R.id.imagePreview);
    }


    public void TurnUpThePictureSelector(View view) {
//        限数量的多选(比喻最多9张)
        ImageSelector.builder()
                .useCamera(true) // 设置是否使用拍照
                .setSingle(false)  //设置是否单选
                .setMaxSelectCount(9) // 图片的最大选择数量，小于等于0时，不限数量。
//                .setSelected(selected) // 把已选的图片传入默认选中。
                .setViewImage(true) //是否点击放大图片查看,，默认为true
                .start(this, REQUEST_CODE); // 打开相册
//        //单选
//        ImageSelector.builder()
//                .useCamera(true) // 设置是否使用拍照
//                .setSingle(true)  //设置是否单选
//                .setViewImage(true) //是否点击放大图片查看,，默认为true
//                .start(this, REQUEST_CODE); // 打开相册

    }

    private void initData() {
        if (images != null)
            mViewDatas = new ArrayList<>();
        mAdapter.setmImages(images);
        imagePreview.setImageData(images);
        imagePreview.showIndex(true);
        for (int i = 0, len = images.size(); i < len; i++) {
            ViewData viewData = new ViewData();
            mViewDatas.add(viewData);
        }
        mScreenSize = ImageViewerUtil.getScreenSize(this);
        for (int i = 0, len = mViewDatas.size(); i < len; i++) {
            ViewData viewData = new ViewData();
            viewData.setTargetX(ImageViewerUtil.dp2px(this, 10));
            viewData.setTargetWidth(mScreenSize.x - ImageViewerUtil.dp2px(this, 20));
            viewData.setTargetHeight(ImageViewerUtil.dp2px(this, 200));
            mViewDatas.set(i, viewData);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE && data != null) {
            //获取选择器返回的数据
            images = data.getStringArrayListExtra(ImageSelector.SELECT_RESULT);
            initData();


            imagePreview.setViewData(mViewDatas);
            mAdapter.setOnItemClickCallback(new RVImageAdapter.OnItemClickCallback() {
                @Override
                public void OnItemClick(int position, ImageView imageView) {
                    imagePreview.setStartPosition(position);

                    imagePreview.setImageLoader(new ImageLoader() {
                        @Override
                        public void displayImage(final int position, Object source, final ImageView imageView) {
                            Glide.with(MainActivity.this).load(source).into(new SimpleTarget<Drawable>() {
                                @Override
                                public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                                    imageView.setImageDrawable(resource);
                                    mViewDatas.get(position).setImageWidth(resource.getIntrinsicWidth());
                                    mViewDatas.get(position).setImageHeight(resource.getIntrinsicHeight());
                                }
                            });
                        }
                    });
                    imagePreview.watch();
                }
            });

        }
    }
}
